package org.dromara.retail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.retail.domain.CrmSaleReport;
import org.dromara.retail.domain.bo.CrmSaleReportBo;
import org.dromara.retail.domain.vo.CrmSaleReportVo;
import org.dromara.retail.mapper.CrmSaleReportMapper;
import org.dromara.retail.service.ICrmSaleReportService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 销售日报Service业务层处理
 *
 * @author Lion Li
 * @Date 2024-05-06
 */
@RequiredArgsConstructor
@Service
public class CrmSaleReportServiceImpl implements ICrmSaleReportService {

    private final CrmSaleReportMapper baseMapper;

    /**
     * 查询销售日报
     */
    @Override
    public CrmSaleReportVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询销售日报列表
     */
    @Override
//    public TableDataInfo<CrmSaleReportVo> queryPageList(CrmSaleReportBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<CrmSaleReport> lqw = buildQueryWrapper(bo);
//        lqw.orderByDesc(CrmSaleReport::getSaleTime);
//        BigDecimal salemoneys = baseMapper.selectList(lqw)
//            .stream()
//            .map(CrmSaleReport::getSaleMoney)
//            .reduce(BigDecimal.ZERO, BigDecimal::add);
//
//        Page<CrmSaleReportVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        return TableDataInfo.builds(result,salemoneys);
//    }
    public TableDataInfo<CrmSaleReportVo> queryPageList(CrmSaleReportBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CrmSaleReport> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(CrmSaleReport::getSaleTime);
        Page<CrmSaleReportVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询销售日报列表
     */
    @Override
    public List<CrmSaleReportVo> queryList(CrmSaleReportBo bo) {
        LambdaQueryWrapper<CrmSaleReport> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CrmSaleReport> buildQueryWrapper(CrmSaleReportBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CrmSaleReport> lqw = Wrappers.lambdaQuery();
        lqw.between(params.get("beginSaleTime") != null && params.get("endSaleTime") != null,
            CrmSaleReport::getSaleTime ,params.get("beginSaleTime"), params.get("endSaleTime"));
        lqw.eq(bo.getSaleMoney() != null, CrmSaleReport::getSaleMoney, bo.getSaleMoney());
        lqw.eq(bo.getOtherMoney() != null, CrmSaleReport::getOtherMoney, bo.getOtherMoney());
        lqw.eq(bo.getGoodsMoney() != null, CrmSaleReport::getGoodsMoney, bo.getGoodsMoney());
        lqw.eq(bo.getAgentMoney() != null, CrmSaleReport::getAgentMoney, bo.getAgentMoney());
        lqw.eq(bo.getOtherExpenditure() != null, CrmSaleReport::getOtherExpenditure, bo.getOtherExpenditure());
        lqw.eq(bo.getBankMoney() != null, CrmSaleReport::getBankMoney, bo.getBankMoney());
        lqw.eq(bo.getExpenditure() != null, CrmSaleReport::getExpenditure, bo.getExpenditure());
        lqw.eq(bo.getSaleProfit() != null, CrmSaleReport::getSaleProfit, bo.getSaleProfit());
        lqw.eq(bo.getGrossProfit() != null, CrmSaleReport::getGrossProfit, bo.getGrossProfit());
        lqw.eq(bo.getDifference() != null, CrmSaleReport::getDifference, bo.getDifference());
        return lqw;
    }

    /**
     * 新增销售日报
     */
    @Override
    public Boolean insertByBo(CrmSaleReportBo bo) {
        CrmSaleReport add = MapstructUtils.convert(bo, CrmSaleReport.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改销售日报
     */
    @Override
    public Boolean updateByBo(CrmSaleReportBo bo) {
        CrmSaleReport update = MapstructUtils.convert(bo, CrmSaleReport.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CrmSaleReport entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除销售日报
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
